package org.lzx.converter;

import java.io.File;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.lzx.converter.file.GenerateClassStructure;

/**
 * 
 * @author swagner
 * @date 29.06.2008
 *
 */
public class ParseLzx {
	
	private static final Log log = LogFactory.getLog(ParseLzx.class);
	
	private static ParseLzx instance;

	private ParseLzx() {}

	public static synchronized ParseLzx getInstance() {
		if (instance == null) {
			instance = new ParseLzx();
		}
		return instance;
	}
	
	public void parse(File f) throws DocumentException {
        SAXReader reader = new SAXReader();
        
        //log.debug("Parsing: "+f.getAbsolutePath());
        Document document = reader.read(f);
        this.parseRoot(document);
    }
	
	public void parseRoot(Document document) throws DocumentException {

        Element root = document.getRootElement();

        // iterate through child elements of root with element name "class"
        for ( Iterator i = root.elementIterator( "class" ); i.hasNext(); ) {
            Element classItem = (Element) i.next();
            // do something
            //log.debug("classItem: "+classItem.getName());
            
            String className = classItem.attribute("name").getText();
            //log.debug("className: "+className);
            GenerateClassStructure.currentClassNameList.add(className);
        }

       
     }

}
